﻿using LDLib;
using OpenTK;
using OpenTK.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LD27
{
    /// <summary>
    /// Main class for the game.
    /// </summary>
    class Program : Engine
    {
        /// <summary>
        /// Entry point.
        /// </summary>
        /// <param name="args">Command line arguments</param>
        static void Main(string[] args)
        {
            // 'using' makes the OpenGL context to dispose correctly
            using(Program p = new Program("Ludum Dare 27 :)"))
            {
                
                // You can also call p.Run(60.0, 60.0) to set the update frequency too.
                p.Run(60.0); // 60.0 here is the render frequency (FPS)
            }
        }

        /// <summary>
        /// Constructs a new 'Program' object. Don't do any resource loading yet!
        /// </summary>
        /// <param name="title">The title text for the window</param>
        public Program(string title) : base(title)
        {

        }

        GameObject go;

        /// <summary>
        /// Load your resources here. Don't remove the base.OnLoad() call!
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            // Let the engine load its own stuff first
            base.OnLoad(e);

            // Create a new scene
            Engine.CurrentScene = new Scene("Scene");

            // Create a new game object, set some properties and add it to the scene
            go = new GameObject("A simple plane");
            go.Mesh = MeshTools.CreatePlaneXY(Vector2.One * 3f);
            go.Material = new Material("Material", R.Shaders["Colored Shader"]);
            go.Material.Set("color", new Vector4(1f, 0f, 1f, 1f));
            Engine.CurrentScene.AddGameObject(go);

            // Create a simple GUI text
            GUIText guitext = new GUIText(AssetLoader.LoadFont("GUI Font", "calibri_32px.fnt"), "Hello, Ludum Dare!");
            guitext.Alignment = GUIText.Align.Center;
            guitext.Position = new Vector2(Properties.ScreenWidth / 2f, Properties.ScreenHeight / 2f - 64f);
            guitext.Size = new Vector2(512, 256);
            GUI.guiElements.Add(guitext);
        }

        /// <summary>
        /// Here you should put your game logic :)
        /// </summary>
        /// <param name="dt">Time since the last frame</param>
        protected override void OnUpdate(double dt)
        {
            // This is how you can read input from the user. This snippet
            // here moves the magenta square sideways when the arrow keys
            // are pressed.
            if(Input.IsKeyPressed(Key.Left))
            {
                go.LocalTransform.Location -= new Vector3(0.1f, 0f, 0f);
            }
            if(Input.IsKeyPressed(Key.Right))
            {
                go.LocalTransform.Location += new Vector3(0.1f, 0f, 0f);
            }
        }
    }
}
